﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Casemeet.ADs;
using VK.Comm;

namespace VK.Casemeet.Web.Admin.ADs
{
    public partial class ADEdit : System.Web.UI.Page
    {
        public int ADID = 0;
        public string imgUrl = "null.jpg";

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/Admin/Home.aspx"},
                new MapPath{ Text="广告",Url="Index.aspx"},
                new MapPath{ Text="编辑广告",Url="#"}
            };

            ADID = Util.GetQueryInt("ADID");

            if (!IsPostBack)
            {
                ADHelper.BindTargetRAD(radTarget);
                LoadData();
            }
        }

        internal void LoadData()
        {
            if (ADID != 0)
            {
                ADEntity AD = ADBLL.GetAD(ADID);
                txtDescription.Text = AD.Description;
                txtWidth.Text = AD.Width.ToString();
                txtHeight.Text = AD.Height.ToString();
                txtUrl.Text = AD.Url;
                radTarget.SelectedValue = AD.Target.ToString();
                chkIsEnable.Checked = AD.IsEnable;
                imgUrl = AD.Image;
                txtCode.Text = AD.Code;
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            ADEntity AD = LoadEntity();
            AD.ADID = ADID;

            if (ADBLL.UpdateAD(AD) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected ADEntity LoadEntity()
        {
            ADEntity AD = ADBLL.GetAD(ADID);
            AD.Code = txtCode.Text;
            AD.Target = int.Parse(radTarget.SelectedValue);
            AD.Description = txtDescription.Text.Trim();
            AD.Width = int.Parse(txtWidth.Text);
            AD.Height = int.Parse(txtHeight.Text);
            AD.Url = txtUrl.Text;
            AD.IsEnable = chkIsEnable.Checked;
            AD.Image = Util.GetFormString("hidImgUrl");
            return AD;
        }
    }
}